package cn.liangjintang.webserver;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
	/**
	 * 正则表达式判断IP正确性
	 * @param ip
	 * @return
	 */
	public static boolean IPisCorrect(String ip) {// 正则表达式判断IP正确性
											// import java.util.regex.*;
		Pattern p = Pattern.compile(
				"^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$");
		Matcher m = p.matcher(ip);
		boolean b = m.matches();

		return b;
	}
	public static boolean isIpv4(String ip){
		String regex = "^((\\d|[1-9]\\d|1\\d\\d|2([0-4]\\d|5[0-5]))\\.){4}$";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(ip);
		boolean b = m.matches();
		return b;
	}

	public static boolean isIpv6(String ip){
		String regex = "^(([\\da-fA-F]{1,4}):){8}$";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(ip);
		boolean b = m.matches();
		return b;
	}
}
